<?php

namespace app\common\controller;

use app\api\model\MemberLogRelation;
use app\api\model\Order;
use think\cache\driver\Redis;
use think\Controller;
use think\Db;

class PaySuccess extends Controller
{
    /**
     * 支付完成逻辑处理
     */
    public function setOrderStatus($data)
    {
        $order = new Order();
        $MemberLogRelation = new MemberLogRelation();
        $res = $order->updateOrder($data);
        if($res['code'] == 0){
            $logData = $order->getOrderInfo(['pay_order_number'=>$data['pay_order_number']]);
            $this->gamePayNotify($logData);
            $MemberLogRelation->setFirstPayTime($data['pay_time'],$logData['pay_amount'],$logData['member_pid'],$logData['package_id'],$logData['promote_id'],$logData['game_id'],$logData['channel_id'],$logData['register_time']);
            $MemberLogRelation->setFirstLoginPayTime($data['pay_time'],$logData['member_id'],$logData['member_pid'],$logData['is_box']);
            $kafka = new Kafka();
            $className = 'cqyq_order';
            $logData['first_pay_time'] = $MemberLogRelation->getFirstPayTime($data['pay_time'],$logData['member_pid'],$logData['package_id'],$logData['promote_id']);
            $registerTime = $MemberLogRelation->getRegisterTime($logData['member_id'],$logData['member_pid'],$logData['is_box']);
            $logData['register_time'] = $registerTime['time'];
            $logData['child_register_time'] = $registerTime['childTime'];
            $kafka->logProducer($className,$logData);
        }
        return $res;
    }
    /**
     * 订单上报
     */
    public function reportOrder($data){
//        write_log('INFO', 'BaseController.class.php','提交参数: '.json_encode($data), 'report_order',true);
//        $res = getHttpPostJson('https://pay-monitor.3975.com/reportOrder',$data);
//        write_log('INFO', 'BaseController.class.php','接口返回: '.$res, 'report_order',true);
//        $res = json_decode($res,true);
//        if(!isset($res['data']) || $res['data'] != 'success'){
            $redis = new Redis(getRedisConfig());
            $redis->lPush('report_order',json_encode($data));
        //}
    }

    /**
     * 通知研发发货
     */
    public function gamePayNotify($data){
        write_log('INFO', 'Notify.php', '通知研发发货-订单号:'.$data['pay_order_number'], 'game_pay', true);
        $gameInfo = Db::name('game')->where(['id'=>$data['game_id']])->find();
        $payNotifyUrl = $gameInfo['pay_notify_url'];
        $appInfo = Db::name('app')->where(['id'=>$gameInfo['app_id']])->field('pay_notify_url,payment_key')->find();
        if(!$payNotifyUrl){
            $payNotifyUrl = $appInfo['pay_notify_url'];
        }
        write_log('INFO', 'Notify.php', '通知地址:'.$payNotifyUrl, 'game_pay', true);
        $arr = array(
            'cp_order_number'   => $data['cp_order_number'],
            'package_id'        => $data['package_id'],
            'user_id'           => $data['member_id'],
            'money'             => $data['cost'],
            'unix_name'         => $data['props_name'],
            'extend'            => $data['extend'],
            'pay_order_number'  => $data['pay_order_number'],
            'is_sandbox'        => $data['is_sandbox'],
            'product_id'        => $data['product_id'],
            'real_money'        => $data['pay_amount'],
            'server_id'         => $data['server_id'],
            'role_id'           => $data['game_role_id']
        );
        $payment_key = $appInfo['payment_key'];
        $arr['sign'] = $this->encrypt_md5($arr,$payment_key);
        write_log('INFO', 'Notify.php', '提交参数:'.json_encode($arr), 'game_pay', true);
        $res = curl_post($payNotifyUrl,$arr);
        write_log('INFO', 'Notify.php', $payNotifyUrl.'--接口响应:'.$res, 'game_pay', true);
        $res = json_decode($res,true);
        $order = new Order();
        $save = array(
            'pay_order_number'  => $data['pay_order_number'],
            'pay_game_msg'      => json_encode($res)
        );
        if($res['code'] === 200){
            $save['pay_game_status'] = 1;
        }
        $order->updateOrder($save);
        return json_encode($res);
    }

    /**
     *对数据进行排序
     */
    private function arrSort($para){
        ksort($para);
        reset($para);
        return $para;
    }

    /**
     *MD5验签加密
     */
    public function encrypt_md5($param="",$key=""){
        #对数组进行排序拼接
        if(is_array($param)){
            $md5Str = implode($this->arrSort($param));
        }
        else{
            $md5Str = $param;
        }
        $md5 = md5($md5Str . $key);
        write_log('INFO', 'Notify.php', '签名前原串:'.$md5Str . $key, 'game_pay', true);
        return '' === $param ? 'false' : $md5;
    }
}
